package view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import model.Auktion;
import model.Bud;
import utility.ImageToolbox;

/**
 *
 * @author Jeppe og brian
 */
public class VisAuktionPanel extends javax.swing.JPanel {

    private MainGui mainGui;
    private Auktion auktion;
    //Timer til countdown
    private Timer t;
    // animation hammer
    private boolean showAnimation;
    private ArrayList<Image> hammerAnimation;
    private int currentImage;
    private ImageToolbox imgTB;
    private double currentbud;

    /**
     * Creates new form VisAuktion
     */
    public VisAuktionPanel(MainGui mainGui) {
        initComponents();
        currentImage = 0;
        currentbud = 100;
        jLabel_budStoerrelse.setText("" + currentbud);
        hammerAnimation = new ArrayList();
        ClassLoader cl = this.getClass().getClassLoader();
        for (int i = 0; i < 4; i++) {
            hammerAnimation.add(new ImageIcon(cl.getResource("images/hammer/hammer000" + i + ".png")).getImage());
        }

        this.showAnimation = false;
        this.mainGui = mainGui;
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nextImage();
                opdater();
                repaint();
            }
        });
        timer.start();
        
        t = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                calculateTimeLeft();
            }
        });

    }

    public void calculateTimeLeft() {
        Calendar first = Calendar.getInstance();
        Calendar second = auktion.getSluttidspunkt();

        long diff = second.getTimeInMillis() - first.getTimeInMillis();

        long diffSeconds = diff / 1000 % 60;

        long minutesLeft = diff / (60 * 1000) % 60;

        long hoursLeft = diff / (60 * 60 * 1000) % 24;

        long daysLeft = diff / (24 * 60 * 60 * 1000);

        countdownLabel.setText(daysLeft + " dage " + hoursLeft + ":" + minutesLeft + ":" + diffSeconds);

    }

    public Image getNextImage() {
        return hammerAnimation.get(currentImage);
    }

    public void nextImage() {
        currentImage = currentImage + 1;

        if (currentImage > 3) {
            currentImage = 0;
        }
    }

    public void opdater() {
        opdaterPanel(auktion);
    }

    public void opdaterPanel(Auktion enAuktion) {
        if (enAuktion != null) {
            setAuktion(enAuktion);

            if (auktion.getSluttidspunkt().getTimeInMillis() <= Calendar.getInstance().getTimeInMillis()) {
                afgivBudbtn.setEnabled(false);
                statusLabel.setText("Auktionen er udløbet.");
                t.stop();
                countdownLabel.setVisible(false);
                tidtilbageLabel.setVisible(false);
            } else {
                afgivBudbtn.setEnabled(true);
                statusLabel.setText("");
                countdownLabel.setVisible(true);
                tidtilbageLabel.setVisible(true);
            }

            billedeLabel.setIcon(auktion.getVareIcon());
            vareTitelLabel.setText(auktion.getVare().getNavn());

            beskrivelseTxtArea.setText(auktion.getVare().getBeskrivelse());
                        
            setAfgivBud(true);

            budHistorikJlist.setListData(auktion.getReversedBudHistorik().toArray());

        }

    }

    public Auktion getAuktion() {
        return auktion;
    }

    private void setAuktion(Auktion auktion) {
        this.auktion = auktion;
    }

    /**
     * Bud sættes til true hvis metoden skal sætte txtfeltet +100 og false hvis
     * man vil sætte txtfeltet til -100.
     *
     * @param bud
     */
    private void setAfgivBud(boolean bud) {
            double senestebud = Math.round(auktion.getSenesteBud().getBeløb());
            double mindstebud = senestebud + currentbud;            
            mindsteBud.setText((mindstebud) + "");
            jLabel_senestebud.setText((senestebud) + "");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        billedeLabel = new javax.swing.JLabel(){
            public void paint (Graphics g){
                super.paint(g);
                imgTB.drawImage(g, auktion.getVareIcon(), 0, 0, billedeLabel.getSize().width, billedeLabel.getSize().height);
            }
        };
        vareTitelLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beskrivelseTxtArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        budHistorikJlist = new javax.swing.JList();
        senesteBudPlusbtn = new javax.swing.JButton();
        senesteBudMinusbtn = new javax.swing.JButton();
        mindsteBud = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        afgivBudbtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        countdownLabel = new javax.swing.JLabel();
        tidtilbageLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_senestebud = new javax.swing.JLabel();
        jLabel_budStoerrelse = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        billedeLabel.setBackground(new java.awt.Color(153, 255, 153));
        add(billedeLabel);
        billedeLabel.setBounds(10, 40, 250, 220);

        vareTitelLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        vareTitelLabel.setText("VareTitel");
        add(vareTitelLabel);
        vareTitelLabel.setBounds(350, 40, 340, 29);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setAutoscrolls(true);

        beskrivelseTxtArea.setEditable(false);
        beskrivelseTxtArea.setColumns(20);
        beskrivelseTxtArea.setRows(5);
        beskrivelseTxtArea.setAutoscrolls(false);
        beskrivelseTxtArea.setBorder(null);
        jScrollPane1.setViewportView(beskrivelseTxtArea);

        add(jScrollPane1);
        jScrollPane1.setBounds(350, 100, 342, 160);

        jLabel3.setText("Beskrivelse:");
        add(jLabel3);
        jLabel3.setBounds(350, 80, 120, 14);

        budHistorikJlist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(budHistorikJlist);

        add(jScrollPane3);
        jScrollPane3.setBounds(10, 376, 680, 116);

        senesteBudPlusbtn.setBackground(new java.awt.Color(0, 204, 0));
        senesteBudPlusbtn.setText("+");
        senesteBudPlusbtn.setToolTipText("Budprisen tillægges 100 kr.");
        senesteBudPlusbtn.setContentAreaFilled(false);
        senesteBudPlusbtn.setOpaque(true);
        senesteBudPlusbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                senesteBudPlusbtnActionPerformed(evt);
            }
        });
        add(senesteBudPlusbtn);
        senesteBudPlusbtn.setBounds(290, 290, 41, 23);

        senesteBudMinusbtn.setBackground(new java.awt.Color(255, 51, 51));
        senesteBudMinusbtn.setText("-");
        senesteBudMinusbtn.setToolTipText("Budprisen fratrækkes 100 kr.");
        senesteBudMinusbtn.setContentAreaFilled(false);
        senesteBudMinusbtn.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        senesteBudMinusbtn.setOpaque(true);
        senesteBudMinusbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                senesteBudMinusbtnActionPerformed(evt);
            }
        });
        add(senesteBudMinusbtn);
        senesteBudMinusbtn.setBounds(290, 320, 41, 23);

        mindsteBud.setEditable(false);
        mindsteBud.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        mindsteBud.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        mindsteBud.setOpaque(false);
        add(mindsteBud);
        mindsteBud.setBounds(10, 292, 230, 50);

        jLabel4.setText("Seneste bud");
        add(jLabel4);
        jLabel4.setBounds(10, 356, 60, 14);

        afgivBudbtn.setText("Afgiv Bud");
        afgivBudbtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                afgivBudbtnMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                afgivBudbtnMouseExited(evt);
            }
        });
        afgivBudbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afgivBudbtnActionPerformed(evt);
            }
        });
        add(afgivBudbtn);
        afgivBudbtn.setBounds(350, 305, 79, 23);

        jButton1.setText("Tilbage til auktioner");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(20, 10, 240, 23);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Kr.");
        add(jLabel1);
        jLabel1.setBounds(250, 295, 30, 40);

        statusLabel.setForeground(new java.awt.Color(255, 51, 51));
        statusLabel.setText("StatusLabel");
        add(statusLabel);
        statusLabel.setBounds(350, 340, 220, 30);

        countdownLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        add(countdownLabel);
        countdownLabel.setBounds(460, 300, 230, 40);

        tidtilbageLabel.setText("Tid tilbage:");
        add(tidtilbageLabel);
        tidtilbageLabel.setBounds(460, 280, 140, 14);

        jLabel2.setText("Seneste");
        add(jLabel2);
        jLabel2.setBounds(20, 260, 50, 14);

        jLabel_senestebud.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel_senestebud.setText("0");
        add(jLabel_senestebud);
        jLabel_senestebud.setBounds(101, 260, 140, 20);

        jLabel_budStoerrelse.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel_budStoerrelse.setText("0");
        add(jLabel_budStoerrelse);
        jLabel_budStoerrelse.setBounds(251, 260, 80, 14);
    }// </editor-fold>//GEN-END:initComponents

    private void senesteBudPlusbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_senesteBudPlusbtnActionPerformed
       currentbud = currentbud + 100;
       jLabel_budStoerrelse.setText("" + currentbud);
        double senestebud = Math.round(Double.parseDouble( jLabel_senestebud.getText() ));
        double mindstebud = senestebud + currentbud;
        mindsteBud.setText(""+mindstebud);
       
    }//GEN-LAST:event_senesteBudPlusbtnActionPerformed

    private void senesteBudMinusbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_senesteBudMinusbtnActionPerformed
        if (currentbud - 100 >= 100) {
            currentbud = currentbud - 100;
            jLabel_budStoerrelse.setText("" + currentbud);
                    double senestebud = Math.round(auktion.getSenesteBud().getBeløb());
        double mindstebud = senestebud + currentbud;
        mindsteBud.setText(""+mindstebud);
        } else {
            // udskriv kan ikke gøre buddet mindre
        }

    }//GEN-LAST:event_senesteBudMinusbtnActionPerformed

    private void afgivBudbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afgivBudbtnActionPerformed

        if (mainGui.getAktivbruger() == null) {
            JOptionPane.showMessageDialog(null, "Du skal være logget ind for at afgive bud");
        } else if (mainGui.getAktivbruger() != null) {
            auktion.tilføjTilBudHistorik(new Bud(Calendar.getInstance(), Double.parseDouble(jLabel_senestebud.getText())+currentbud, mainGui.getAktivbruger()));
            currentbud = 100;
            System.out.println("nulstiller currentbud");
            opdaterPanel(auktion);

        }
    }//GEN-LAST:event_afgivBudbtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        mainGui.showAuktionsliste();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void afgivBudbtnMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_afgivBudbtnMouseEntered
        this.showAnimation = true;
    }//GEN-LAST:event_afgivBudbtnMouseEntered

    private void afgivBudbtnMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_afgivBudbtnMouseExited
        this.showAnimation = false;
    }//GEN-LAST:event_afgivBudbtnMouseExited

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        calculateTimeLeft();
        t.start();
    }//GEN-LAST:event_formComponentShown

    public void paint(Graphics g) {
        super.paint(g);

        if (showAnimation) {
            Image image = getNextImage();
            if (image != null) {
                g.drawImage(image, afgivBudbtn.getX(), afgivBudbtn.getY() - 100, null);
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton afgivBudbtn;
    private javax.swing.JTextArea beskrivelseTxtArea;
    private javax.swing.JLabel billedeLabel;
    private javax.swing.JList budHistorikJlist;
    private javax.swing.JLabel countdownLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel_budStoerrelse;
    private javax.swing.JLabel jLabel_senestebud;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField mindsteBud;
    private javax.swing.JButton senesteBudMinusbtn;
    private javax.swing.JButton senesteBudPlusbtn;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JLabel tidtilbageLabel;
    private javax.swing.JLabel vareTitelLabel;
    // End of variables declaration//GEN-END:variables
}
